package com.ruoyi.web.controller.aggregation;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.aggregation.dto.PageDTO;
import com.ruoyi.system.domain.aggregation.SalesmanAudit;
import com.ruoyi.system.service.aggregation.ISalesmanAuditService;

/**
 * 业务员审核Controller
 * 
 * @author ruoyi
 * @date 2025-01-10
 */
@RestController
@RequestMapping("/agg/salesmanAudit")
public class SalesmanAuditController
{
    @Autowired
    private ISalesmanAuditService salesmanAuditService;

    /**
     * 分页查询业务员审核
     */
    @PostMapping("/page")
    public AjaxResult page(@RequestBody PageDTO pageDTO)
    {
        try
        {
            PageHelper.startPage(pageDTO.getPageIndex().intValue(), pageDTO.getPageSize().intValue());
            SalesmanAudit query = new SalesmanAudit();
            List<SalesmanAudit> list = salesmanAuditService.selectSalesmanAuditList(query);
            PageInfo<SalesmanAudit> pageInfo = new PageInfo<>(list);
            return AjaxResult.success(pageInfo);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 添加业务员审核（总公司/区域代理提交）
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SalesmanAudit salesmanAudit)
    {
        try
        {
            salesmanAuditService.insertSalesmanAudit(salesmanAudit);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 编辑业务员审核
     */
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody SalesmanAudit salesmanAudit)
    {
        try
        {
            salesmanAuditService.updateSalesmanAudit(salesmanAudit);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 审核业务员（总公司审核）
     * 
     * @param id 审核记录ID
     * @param statue 审核状态：2-通过，3-拒绝
     * @param reason 拒绝原因（拒绝时必填）
     */
    @PostMapping("/check")
    public AjaxResult check(@RequestParam Long id, @RequestParam Integer statue, 
                           @RequestParam(required = false) String reason)
    {
        try
        {
            salesmanAuditService.checkSalesmanAudit(id, statue, reason);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 查看业务员审核详情
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(@RequestParam Long id)
    {
        try
        {
            SalesmanAudit audit = salesmanAuditService.selectSalesmanAuditById(id);
            return AjaxResult.success(audit);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除业务员审核
     */
    @PostMapping("/delete")
    public AjaxResult delete(@RequestParam Long id)
    {
        try
        {
            salesmanAuditService.deleteSalesmanAuditById(id);
            return AjaxResult.success();
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}

